/**
 * Copyright (c) 2010 Simple Graph Tools contributors. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id$
 */
package org.sgt.coredef.util;

import org.sgt.coredef.DescribedObject;
import org.sgt.coredef.Node;

/**
 * A Route (in a Graph). It the couple {source Node ; destination Node}. <BR/>
 * The {@link #equals(Object)} and {@link #hashCode()} have been modified to
 * reflect the fact that when both source and destination are the same nodes,
 * the route is the same object.
 * 
 * @author Anne BUTTIGHOFFER
 * 
 */
public class Route {

	/**
	 * The source of this route.
	 */
	private Node source;

	/**
	 * The destination of this route.
	 */
	private Node destination;

	/**
	 * Constructor.
	 * @param start soure of the route.
	 * @param end destination of the route.
	 */
	public Route(final Node start, final Node end) {
		source = start;
		destination = end;
	}

	/**
	 * @return {@link #source}.
	 */
	public final Node getSource() {
		return source;
	}

	/**
	 * @return {@link #destination}.
	 */
	public final Node getDestination() {
		return destination;
	}

	@Override
	public final String toString() {
		StringBuilder output = new StringBuilder();
		output.append("Source : ").append(((DescribedObject) source).getName())
				.append(" -> ");
		output.append("Destination : ").append(
				((DescribedObject) destination).getName());
		return output.toString();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public final int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((destination == null) ? 0 : destination.hashCode());
		result = prime * result + ((source == null) ? 0 : source.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public final boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Route)) {
			return false;
		}
		Route other = (Route) obj;
		if (destination == null) {
			if (other.destination != null) {
				return false;
			}
		} else if (!destination.equals(other.destination)) {
			return false;
		}
		if (source == null) {
			if (other.source != null) {
				return false;
			}
		} else if (!source.equals(other.source)) {
			return false;
		}
		return true;
	}

}
